package org.dtrd.modules.food.controller;


import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.common.aspect.annotation.AutoLog;
import org.dtrd.modules.food.entity.bean.FoodMatchingCategoryDetail;
import org.dtrd.modules.food.entity.bean.FoodMatchingCategoryListInfo;
import org.dtrd.modules.food.entity.request.FoodMatchingCategoryListRequest;
import org.dtrd.modules.food.service.IDtrdExtEmsYwFoodMatchingCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static org.dtrd.common.constant.CommonConstant.*;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_OPERATION_FAILURE;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_PARAMS_MISSING;

/**
 * <p>
 * 配餐分类 前端控制器
 * </p>
 * @author QR
 * @since 2021-12-09
 */
@Api(tags = "食物库")
@RestController
@RequestMapping("/100/16/")
public class FoodMatchingCategoryController extends BaseController {

    @Autowired
    private IDtrdExtEmsYwFoodMatchingCategoryService foodMatchingCategoryService;

    @ApiOperation(value = "查询配餐分类详情", response = FoodMatchingCategoryDetail.class)
    @GetMapping("1001628")
    @AutoLog(value="查询配餐分类详情", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getFoodMatchingCategory(@RequestParam Integer id){
        FoodMatchingCategoryDetail info = foodMatchingCategoryService.getFoodMatchingCategory(id);
        return Result.OK(info);
    }

    @ApiOperation(value = "查询配餐分类列表", response = FoodMatchingCategoryListInfo.class)
    @PostMapping("1001627")
    @AutoLog(value="查询配餐分类列表", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getFoodMatchingCategoryList(@RequestBody FoodMatchingCategoryListRequest request){
        List<FoodMatchingCategoryListInfo> list = foodMatchingCategoryService.getFoodMatchingCategoryList(request);
        return Result.OK(list);
    }

    @ApiOperation("新增或修改配餐分类")
    @PostMapping("1001629")
    @AutoLog(value="新增或修改配餐分类", operateType = OPERATE_TYPE_SAVE_OR_UPDATE)
    public Result<?> saveOrUpdateFoodMatchingCategory(@RequestBody FoodMatchingCategoryDetail dto){
        if (StrUtil.isBlank(dto.getCategoryName())) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = foodMatchingCategoryService.saveOrUpdateFoodMatchingCategory(dto);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }

    @ApiOperation("删除配餐分类")
    @DeleteMapping("1001630")
    @AutoLog(value="删除配餐分类", operateType = OPERATE_TYPE_DELETE)
    public Result<?> removeFoodMatchingCategory(@RequestParam Integer id){
        if(isIntParamInvalid(id)) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = foodMatchingCategoryService.removeFoodMatchingCategory(id);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }
}
